package com.winning.pmph.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.winning.pmph.config.CommonConfig;

public class FfmpegUtil {
	
	public static String aa = "E:\\soft\\ffmpeg\\bin\\ffmpeg.exe";

	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		try {
			// 获取第一帧֡
			//getThumb("E:\\aa\\视频1  全机器人根治性全胃切除术（Uncut Roux-en-Y吻合术；手工缝合）.mp4", "E:\\aa\\222223.png", 800, 600, 0, 0, 1);
			//getBitrate("D:\\aa\\血管外科手术学（第2版）\\media\\视频1  颈动脉闭塞内膜剥脱术  刘小平.mp4");
			//processUnVideoBV("D:\\aa\\血管外科手术学（第2版）\\media\\视频1  颈动脉闭塞内膜剥脱术  刘小平.mp4", "D:\\aa\\TEST_500K.mp4", "500K");
			//convertToStandard("E:/aa/1505105592522399746.mp4", "e:/aa/11_480.mp4");
			System.out.println("end");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void convertToStandard(String videoFilename, String descFileName)
			throws IOException, InterruptedException {

		ProcessBuilder processBuilder = new ProcessBuilder(CommonConfig.ffmpegUrl, "-i", videoFilename, "-vcodec", "h264", "-b:v", "500K", "-s", "720*480", descFileName);

		Process process = processBuilder.start();

		InputStream stderr = process.getErrorStream();
		InputStreamReader isr = new InputStreamReader(stderr);
		BufferedReader br = new BufferedReader(isr);
		String line;
		while ((line = br.readLine()) != null)
			;
		process.waitFor();

		if (br != null)
			br.close();
		if (isr != null)
			isr.close();
		if (stderr != null)
			stderr.close();
	}

	public static void processUnVideoR(String videoFilename, String descFileName)
			throws IOException, InterruptedException {

		ProcessBuilder processBuilder = new ProcessBuilder(CommonConfig.ffmpegUrl, "-i", videoFilename, "-r", "20", descFileName);

		Process process = processBuilder.start();

		InputStream stderr = process.getErrorStream();
		InputStreamReader isr = new InputStreamReader(stderr);
		BufferedReader br = new BufferedReader(isr);
		String line;
		while ((line = br.readLine()) != null)
			;
		process.waitFor();

		if (br != null)
			br.close();
		if (isr != null)
			isr.close();
		if (stderr != null)
			stderr.close();
	}

	public static void processUnVideoBV(String videoFilename, String descFileName, String ma)
			throws IOException, InterruptedException {

		ProcessBuilder processBuilder = new ProcessBuilder(CommonConfig.ffmpegUrl, "-i", videoFilename, "-b:v", ma, descFileName);

		Process process = processBuilder.start();

		InputStream stderr = process.getErrorStream();
		InputStreamReader isr = new InputStreamReader(stderr);
		BufferedReader br = new BufferedReader(isr);
		String line;
		while ((line = br.readLine()) != null)
			;
		process.waitFor();

		if (br != null)
			br.close();
		if (isr != null)
			isr.close();
		if (stderr != null)
			stderr.close();
	}

	public static String getBitrate(String filePath) {
		try {
			ProcessBuilder processBuilder = new ProcessBuilder(CommonConfig.ffprobeUrl, "-show_format","-i",filePath);

			Process process = processBuilder.start();

			InputStream stderr = process.getErrorStream();
			InputStreamReader isr = new InputStreamReader(stderr);
			BufferedReader br = new BufferedReader(isr);
			String line;
			StringBuffer sb = new StringBuffer();
			while ((line = br.readLine()) != null) {
				sb.append(line);
				System.out.println(line);
			}
			process.waitFor();

			if (br != null)
				br.close();
			if (isr != null)
				isr.close();
			if (stderr != null)
				stderr.close();
			System.out.println(sb.toString());
			JSONObject obj = JSONObject.parseObject(sb.toString());
			
			return obj.getJSONObject("format").getString("bit_rate");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	/****
	 * 获取指定时间内的封面图片
	 * 
	 * @param videoFilename:视频路径
	 * @param thumbFilename:图片保存路径
	 * @param width:图片长
	 * @param height:图片宽
	 * @param hour:指定时
	 * @param min:指定分
	 * @param sec:指定秒
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void getThumb(String videoFilename, String thumbFilename, int width, int height, int hour, int min,
			float sec) throws IOException, InterruptedException {
		System.out.println("******切图*****"+thumbFilename);
		ProcessBuilder processBuilder = new ProcessBuilder(CommonConfig.ffmpegUrl, "-y", "-i", videoFilename, "-vframes", "1", "-ss",
				hour + ":" + min + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height, "-an", thumbFilename);

		Process process = processBuilder.start();

		InputStream stderr = process.getErrorStream();
		InputStreamReader isr = new InputStreamReader(stderr);
		BufferedReader br = new BufferedReader(isr);
		String line;
		while ((line = br.readLine()) != null)
			;
		process.waitFor();

		if (br != null)
			br.close();
		if (isr != null)
			isr.close();
		if (stderr != null)
			stderr.close();
	}
	

	public static int getResourceType(String fileExtName) {
		String suffix = fileExtName.toLowerCase();
		int resourceType = 0;
		if (".jpg,.jpeg,.png,.bmp".contains(suffix)) {
			resourceType = 1;
		} else if (".mp4,.rm,.rmvb,.wmv,.mov,.avi".contains(suffix)) {
			resourceType = 2;
		} else if (".doc,docx".contains(suffix)) {
			resourceType = 3;
		} else if (".ppt,pptx".contains(suffix)) {
			resourceType = 5;
		} else if (".pdf".contains(suffix)) {
			resourceType = 6;
		} else if (".mp3,wave,wma".contains(suffix)) {
			resourceType = 7;
		}
		return resourceType;
	}
}
